//
//  UMengShare.m
//  danpin
//
//  Created by chuangjia on 27/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "UMengShare.h"
#import "MobClick.h"
#import "UMFeedback.h"
#import "ZShare.h"
#define UMENG_APPKEY @"55c0844267e58ef73e002c02"

@implementation UMengShare
+ (instancetype)sharedInstance
{
    static UMengShare *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[UMengShare alloc] init];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)initSDK:(BOOL)isDebug
{
    [self umengTrack];
}
- (void)umengTrack {

    [MobClick setLogEnabled:NO];  // 打开友盟sdk调试，注意Release发布时需要注释掉此行,减少io消耗
    [MobClick setAppVersion:XcodeAppVersion]; //参数为NSString * 类型,自定义app版本信息，如果不设置，默认从CFBundleVersion里取
    //
    [MobClick startWithAppkey:UMENG_APPKEY reportPolicy:(ReportPolicy) REALTIME channelId:nil];
    [UMFeedback setAppkey:UMENG_APPKEY];
    [MobClick updateOnlineConfig];  //在线参数配置
    
    //    1.6.8之前的初始化方法
    //    [MobClick setDelegate:self reportPolicy:REALTIME];  //建议使用新方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onlineConfigCallBack:) name:UMOnlineConfigDidFinishedNotification object:nil];
    
}
- (void)updateMethod:(NSDictionary *)appInfo {
    NSLog(@"update info %@",appInfo);
}
- (void)onlineConfigCallBack:(NSNotification *)note {
    
    NSLog(@"online config has fininshed and note = %@", note.userInfo);
}
+ (void)event:(NSString *)eventId acc:(NSInteger)accumulation
{
    [MobClick event:eventId acc:accumulation];
}
+(void)userFeedback:(NSString *)str
{
    if (str.length==0) {
        return;
    }
    NSDictionary *userinfo =[ZShare sharedInstance].userInfo;
    
    NSString *user_id =[[NSString stringWithFormat:@"%@",[userinfo objectForKey:@"user_id"]] replaceNullString];
    NSString *nick_name =[[NSString stringWithFormat:@"%@",[userinfo objectForKey:@"nick_name"]] replaceNullString];
    NSString *user_phone =[[NSString stringWithFormat:@"%@",[userinfo objectForKey:@"user_phone"]] replaceNullString];
    
    NSDictionary *postContent = @{@"content":str,
                                  @"gender":@"1",
                                  @"age_group":@"3",
                                  @"phone":user_phone,
                                  @"nickname":nick_name,
                                  @"userid":user_id,
                                  @"type": @"user_reply",
                                  };
    
    [[UMFeedback sharedInstance] post:postContent completion:^(NSError *error) {
        NSLog(@"userFeedback and note = %@", error);
    }];
}
@end
